package com.example.kalaok;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class PlayListAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	
	public PlayListAdapter(Context context){
		mContext = context;
		this.mInflater = LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return MyApplication.gPlayList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		final int pos = position;
		if (convertView == null) {		
			holder=new ViewHolder();  		
			convertView = mInflater.inflate(R.layout.lvitem_selected_song, null);
			holder.name = (TextView)convertView.findViewById(R.id.name);
			holder.artist = (TextView)convertView.findViewById(R.id.artist);
			holder.cut = (Button)convertView.findViewById(R.id.btn_cut);
			holder.insert = (Button)convertView.findViewById(R.id.btn_insert);
			holder.delete = (Button)convertView.findViewById(R.id.btn_delete);
			convertView.setTag(holder);	
		}else {	
			holder = (ViewHolder)convertView.getTag();
		}
		holder.name.setText((String)MyApplication.gPlayList.get(position).get("name"));
		holder.artist.setText((String)MyApplication.gPlayList.get(position).get("artist"));	
		final PlayListAdapter adapter = this;
		holder.cut.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				MyApplication.insertSong(MyApplication.gPlayList.get(pos));
				PlayView playview = (PlayView)((Activity) mContext).findViewById(R.id.surface);
				playview.cutSong();
				adapter.notifyDataSetChanged();
			}
		});
		holder.insert.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				MyApplication.insertSong(MyApplication.gPlayList.get(pos));
				adapter.notifyDataSetChanged();
			}
		});		
		holder.delete.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				MyApplication.deleteSong(MyApplication.gPlayList.get(pos));
				adapter.notifyDataSetChanged();
			}
		});
		return convertView;
	}

	public final class ViewHolder{
		public TextView name;
		public TextView artist;
		public Button cut;
		public Button insert;
		public Button delete;
	}
}
